package com.platform.user.controller;

import com.platform.bean.PageInfo;
import com.platform.bean.Result;
import com.platform.user.dto.AuditItemNoticeDto;
import com.platform.user.entity.ItemClaimNotice;
import com.platform.user.entity.LostHelpNotice;
import com.platform.user.service.ItemAuditService;
import com.platform.user.service.ItemClaimNoticeService;
import com.platform.user.service.LostHelpNoticeService;
import org.json.JSONException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("itemAudit")
public class ItemAuditController {

    @Resource
    private ItemAuditService auditService;
    @Resource
    private LostHelpNoticeService lostHelpNoticeService;
    @Resource
    private ItemClaimNoticeService itemClaimNoticeService;

    @PostMapping("/auditItem")
    public Result<Map<String, Object>> auditItem(@RequestBody Map<String, Object> params) throws JSONException, IOException {
        auditService.auditItem(params);
        return Result.ok("审核成功");
    }

    @PostMapping("/queryUnAuditLostByPage")
    public Result<Map<String, Object>> queryUnAuditLostByPage(@RequestBody Map<String, Object> params) {
        PageInfo<LostHelpNotice> lostHelpNoticePageInfo = auditService.queryUnAuditLostByPage(params);
        return Result.ok(lostHelpNoticePageInfo);
    }

    @PostMapping("/queryUnAuditFoundByPage")
    public Result<Map<String, Object>> queryUnAuditFoundByPage(@RequestBody Map<String, Object> params) {
        PageInfo<ItemClaimNotice> itemClaimNoticePageInfo = auditService.queryUnAuditFoundByPage(params);
        return Result.ok(itemClaimNoticePageInfo);
    }

    @PostMapping("/queryItemByPage")
    public Result<Map<String, Object>> queryItemByPage(@RequestBody Map<String, Object> params) {
        PageInfo<AuditItemNoticeDto> auditItemNoticeDtoPageInfo = auditService.queryItemByPage(params);
        return Result.ok(auditItemNoticeDtoPageInfo);
    }

    @PostMapping("/queryItemPageByUserId")
    private Result<Map<String, Object>> queryItemPageByUserId(@RequestBody Map<String, Object> params) {
        PageInfo<AuditItemNoticeDto> auditItemNoticeDtoPageInfo = auditService.queryItemPageByUserId(params);
        return Result.ok(auditItemNoticeDtoPageInfo);
    }

    @PostMapping("auditItemClaimNotice")
    private Result<Map<String, Object>> auditItemClaimNotice(@RequestBody Map<String, Object> params) {
        PageInfo<AuditItemNoticeDto> auditItemNoticeDtoPageInfo = auditService.queryItemPageByUserId(params);
        return Result.ok(auditItemNoticeDtoPageInfo);
    }

    @PostMapping("withdrawAudit")
    public Result<Map<String, Object>> withdrawAudit(@RequestBody Map<String, Object> params) {
        auditService.withdrawAudit(params);
        return Result.ok("ok");
    }

    @PostMapping("retryAuditLostItem")
    public Result<Map<String, Object>> retryAuditLostItem(@RequestBody Map<String, Object> params) {
        LostHelpNotice lostHelpNotice = auditService.retryAuditLostItem(params);
        return Result.ok("ok");
    }

    @PostMapping("retryAuditFoundItem")
    public Result<Map<String, Object>> retryAuditFoundItem(@RequestBody Map<String, Object> params) {
        ItemClaimNotice itemClaimNotice = auditService.retryAuditFoundItem(params);
        return Result.ok("ok");
    }

    @PostMapping("deleteAudit")
    public Result<Map<String, Object>> deleteAudit(@RequestBody Map<String, Object> params) {
        Integer itemSort = Integer.parseInt(params.get("itemSort").toString());
        // Integer itemId = Integer.parseInt(params.get("itemId").toString());
        auditService.deleteAudit(params);
        if (itemSort == 0) {
            lostHelpNoticeService.deleteByItemId(params);
        } else {
            itemClaimNoticeService.deleteByItemId(params);
        }
        return Result.ok("ok");
    }
}
